package _5thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-06-09 11:26
 */
public class ReentrantLockTest2 {

    static Lock lock=new ReentrantLock();
    public static void main(String[] args) {

        Thread thread1=new Thread("1号"){

            @Override
            public void run() {

                for(int i=0;i<2;i++){
                    test2();
                }

            }
        };

        Thread thread2=new Thread("2号"){

            @Override
            public void run() {

                for(int i=0;i<2;i++){
                    test2();
                }

            }
        };

        thread1.start();
        thread2.start();
    }

    public synchronized static void test1(){

        System.out.println(Thread.currentThread().getName()+"进入了方法");

        synchronized (ReentrantLockTest2.class){
            System.out.println(Thread.currentThread().getName()+"进入了代码块1");

            synchronized (ReentrantLockTest2.class){
                System.out.println(Thread.currentThread().getName()+"进入了代码块2");
            }
        }
    }

    public static void test2(){

        if(lock.tryLock()){
            System.out.println(Thread.currentThread().getName()+"获得了锁1");

            if(lock.tryLock()){
                System.out.println(Thread.currentThread().getName()+"获得了锁2");

                if(lock.tryLock()){
                    System.out.println(Thread.currentThread().getName()+"获得了锁3");


                }
            }

            lock.unlock();
        }


    }
}
